package com.ruoyi.api.service.impl;

import com.github.pagehelper.PageInfo;
import com.ruoyi.api.service.InterceptUtilService;
import com.ruoyi.api.service.InventoryService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.WXShiroUtils;
import com.ruoyi.product.domain.Product;
import com.ruoyi.product.mapper.AICodeMapper;
import com.ruoyi.product.mapper.ProductMapper;
import com.ruoyi.warehousing.domain.Warehousing;
import com.ruoyi.warehousing.mapper.WarehousingMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class InventoryServiceImpl implements InventoryService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private WarehousingMapper warehousingMapper;

    @Autowired
    private AICodeMapper aiCodeMapper;

    @Autowired
    InterceptUtilService interceptUtilService;
//    /**
//     * 截取字符 判断条形码是否正确
//     *
//     * @param barcode 条形码内容
//     * @return 结果
//     */
//    @Override
//    public String intercept(String barcode) {
//        String firstCode = barcode.substring(0, 2);
//        if (!firstCode.equals("01")) {
//            return "0";
//        }
//        // 切除AI编号
//        String nextStr = barcode.substring(2);
//        if (nextStr.length() < 14) {
//            return "";
//        }
//        String code = nextStr.substring(0, 14);
//
//        return code;
//    }

    //    public AjaxResult intercept(String barcode) {
//        String firstCode = barcode.substring(0, 2);
//        if (!firstCode.equals("01")) {
//            return AjaxResult.error("请扫描01开头的AI码");
//        }
//        // 切除AI编号
//        String nextStr = barcode.substring(2);
//        String code = nextStr.substring(0, 14);
////        ProductModel productModel = productMapper.selectProductByModelCode(ShiroUtils.getSysUser().getCompanyId(), code);
////        if (productModel == null) {
////            return "未找到该AI码信息，请确定是否录入该产品的产品AI码信息";
////        }
//        return AjaxResult.success(code);
//    }

    /**
     * 截取字符  截取产品的型号唯一码
     *
     * @param barcode 条形码内容
     * @return 结果
     */

    @Override
    public AjaxResult interceptStr(String barcode) {
        String code = null;
        String firstCode = barcode.substring(0, 2);
        if (!firstCode.equals("01")) {
            return AjaxResult.error("请扫描01开头的AI码", code);
        }
        // 切除AI编号
        String nextStr = barcode.substring(2);
        if (nextStr.length() != 14) {
            return AjaxResult.error("字符串长度与实际不符", code);
        }
        code = nextStr.substring(0, 14);

        return AjaxResult.success("", code);
    }

    /**
     * 查询产品库存信息
     *
     * @param warehousing 查询条件
     * @return 结果
     */
    @Override
//    public List<Product> selectProductInventory(Warehousing warehousing) {
//        warehousing.setDeptId(ShiroUtils.getSysUser().getCompanyId());
////        warehousing.setDeptId(101L);
//        List<Product> products = warehousingMapper.selectProductInventoryLists(warehousing);
//        if (products.size() != 0) {
//            for (Product product : products) {
//                List<Warehousing> warehousingList = product.getWarehousingList();
//                if (warehousingList.size() != 0) {
//                    for (int i = warehousingList.size() - 1; i >= 0; i--) {
//                        if (warehousingList.get(i).getBatch() == null || warehousingList.get(i).getBatch().equals("")) {
//                            warehousingList.remove(i);
//                        }
//                    }
//                }
//            }
//        }
//        return products;
//    }

//    public TableDataInfo selectProductInventory(Warehousing warehousing) {
//        Map<String, Object> map = new HashMap<>();
//        if (warehousing.getModelCode() != null && warehousing.getModelCode() != "") {
//            map.put("modelCode", warehousing.getModelCode());
//            map.put("deptId", ShiroUtils.getSysUser().getCompanyId());
//        } else {
//            map.put("deptId", ShiroUtils.getSysUser().getCompanyId());
//        }
//        // 先查询主表数据Id
//        List<Map<String, Object>> ids = productMapper.getIds(map);
//        TableDataInfo rspData = new TableDataInfo();
//        rspData.setCode(0);
//        warehousing.setIds(ids);
//
//        warehousing.setDeptId(ShiroUtils.getSysUser().getCompanyId());
//
//        if (warehousing.getIds().size() == 0) {
//
//        } else {
//            List<Product> products = warehousingMapper.selectProductInventoryLists(warehousing);
//            if (products.size() != 0) {
//                for (Product product : products) {
//                    List<Warehousing> warehousingList = product.getWarehousingList();
//                    if (warehousingList.size() != 0) {
//                        for (int i = 0; i < warehousingList.size(); i++) {
//                            if (warehousingList.get(i).getBatch() == null || warehousingList.get(i).getBatch().equals("")) {
//                                warehousingList.remove(i);
//                            }
//                        }
//                    }
//                }
//            }
//            rspData.setTotal(new PageInfo(products).getTotal());
//            rspData.setRows(products);
////            rspData.setRows(warehousingMapper.selectProductInventoryLists(warehousing));
//        }
//        return rspData;
//    }

    public TableDataInfo selectProductInventory(Warehousing warehousing) {
        Map<String, Object> map = new HashMap<>();
        SysUser sysUser=SecurityUtils.getLoginUser().getUser();
        if (warehousing.getModelCode() != null && warehousing.getModelCode() != "") {
            map.put("modelCode", warehousing.getModelCode());
            map.put("deptId", sysUser.getCompanyId());
        } else {
            map.put("deptId", sysUser.getCompanyId());
        }
        // 先查询主表数据Id
        List<Map<String, Object>> ids = productMapper.getIds(map);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setTotal(new PageInfo(ids).getTotal());
        warehousing.setIds(ids);
        if (warehousing.getIds().size() == 0) {

        } else {
            List<Product> products = warehousingMapper.selectProductInventoryLists(warehousing);
            if (products.size() != 0) {
                for (Product product : products) {
                    List<Warehousing> warehousingList = product.getWarehousingList();
                    if (warehousingList.size() != 0) {
                        for (int i = 0; i < warehousingList.size(); i++) {
                            if (warehousingList.get(i).getBatch() == null || warehousingList.get(i).getBatch().equals("")) {
                                warehousingList.remove(i);
                            }
                        }
                    }
                }
            }
            rspData.setRows(products);
//           rspData.setRows(warehousingMapper.selectProductInventoryLists(warehousing));
        }
        return rspData;
    }

    /**
     * 查询产品库存信息
     *
     * @param product 查询条件
     * @return 结果
     */
    @Override
    public TableDataInfo selectProductInventoryCount(Product product) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Map<String, Object> map = new HashMap<>();
        if (product.getProductName() != null && product.getProductName() != "") {
            map.put("productName", product.getProductName());
            map.put("productType", product.getProductType());
            map.put("deptId", user.getCompanyId());
        } else {
            map.put("deptId", user.getCompanyId());
        }
        //通过产品的型号查询
        List<Map<String, Object>> ides = productMapper.getIdMap(map);
        if (ides.size() == 0) {
            //通过产品名称查询
            ides = productMapper.getIdss(map);
        }
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setTotal(new PageInfo(ides).getTotal());
        product.setIdes(ides);
        List<Product> products = new ArrayList<>();
        product.setIdes(ides);
        if (ides.size() != 0) {
            rspData.setRows(warehousingMapper.selectProductInventoryCount(product));
        } else {
            rspData.setRows(products);
        }
        return rspData;
    }

//    @Override
//    public List<AICode> getScanData(String[] barCode) {
//        String modelCode="";
//        //查询 唯一码
//        for (int i = 0; i < barCode.length; i++) {
//            String firstCode = barCode[i].substring(0, 2);
//            if (!firstCode.equals("01")) {
//                return "0";
//            }
//            // 切除AI编号
//            String nextStr = barCode[i].substring(2);
//            if (nextStr.length() < 14) {
//                return "0";
//            }
//            String code = nextStr.substring(0, 14);
//        }
//
//
//    }

}
